<?php
final class Database {

	private $db =null;
	private $inflector = null;

	public function __construct() {
		$this->db = new PDO(DRIVER.":dbname=".DB.";host=".HOST, USER, PASS );
	}

	public function select($cond = null, $fields = null, $table) {
		$query = "SELECT ";
		if (is_null($fields)) $query .=" * ";
		else $query .= " ".$fields;
		$query .= " FROM ".$table;
		if (!is_null($cond)) $query .= " WHERE ".$cond;
		$query .= ";";

		$statement = $this->db->prepare($query);
		$statement->execute();
		$result = $statement->fetchAll(PDO::FETCH_ASSOC);
			
		return $result;
	}

	public function insert($fields,$values,$table) {
		$query = "INSERT INTO ".$table." (".$fields.") VALUES(".$values.");";

		$statement = $this->db->prepare($query);
		return $statement->execute();

	}
	
	public function update($fieldsValues,$cond,$table) {
		$query = "UPDATE ".$table. " SET ";
		foreach ($fieldsValues as $field => $value) {
			if(!is_numeric($value)) {
				$query .= $field."='".$value."',"; 	
			} else {
				$query .= $field."=".$value.",";
			}
		}
		$query = substr($query, 0, -1); 
		$query .= " WHERE ".$cond.";";
	
		$statement = $this->db->prepare($query);
		$statement->execute();
	
	}
	
	public function delete($cond,$table) {
		$query = "DELETE FROM ".$table." WHERE ".$cond.";";
		$statement = $this->db->prepare($query);
		$result = $statement->execute();
		if($result) {
			return true;
		} else {
			return $statement->errorCode();
		}
		
	}
}